
package flowchart.nodes;

import java.awt.*;

/**
 *
 * class representing GraphicsConnector
 */
public class GraphicsConnector extends AbstractGraphicsObject {

    /** Creates a new instance of GraphRect */
    private GraphicsObject node1,  node2;
    private boolean selected;

    public GraphicsConnector(String title,GraphicsObject n1, GraphicsObject n2) {
        System.out.println("creating graphics connector");
        node1 = n1;
        node2 = n2;

        Point p1=new Point(node1.getBounds().x,node1.getBounds().y);
        Point p2=new Point(node2.getBounds().x,node2.getBounds().y);

        Polygon poly = new Polygon();
        poly.addPoint(p1.x,p1.y);
        poly.addPoint(p2.x,p2.y);

        this.setBounds(poly.getBounds());

        this.setTitle(title);
    }

    public GraphicsObject getNode1() {
        return node1;
    }

    public GraphicsObject getNode2() {
        return node2;
    }

    public GraphicsConnector(Rectangle r) {
        setBounds((Rectangle) r.clone());
    }

    public Point getStartP(){
       Rectangle re1=node1.getBounds();
       return new Point(re1.x + re1.width/2,re1.y+re1.height/2);
    }

     public Point getEndP(){
            Rectangle re2=node2.getBounds();
            return new Point(re2.x+re2.width/2,re2.y+re2.height/2);
    }

    @Override
    public Rectangle getBounds() {
        Polygon poly = new Polygon();
        poly.addPoint(getStartP().x,getStartP().y);
        poly.addPoint(getEndP().x,getEndP().y);
        return poly.getBounds();
    }


    public void draw(Graphics2D g) {
    
        if (bgColor != null) {
            Color old = g.getColor();
            g.setColor(bgColor);
            g.fill(bounds);
            g.setColor(old);
        }
 
        if (node1!=null&&node2!=null){
        g.drawLine(getStartP().x,getStartP().y,getEndP().x,getEndP().y);

        //drawing title
      FontMetrics fm = g.getFontMetrics();
      int th = fm.getAscent();
      int tw = fm.stringWidth(this.getTitle());
      Rectangle re=this.getBounds();
      g.drawString(this.getTitle(), (re.x+re.width/2)-(tw/2), re.y + (re.height/2));

        }
    }
}
